package com.jfinal.code.eclipse.plugin.generator.db;

/**
 * 支持的数据库类型
 * @author yaolin
 *
 */
public enum DbType {
	
	MySQL("SELECT * FROM `%s` LIMIT 0", "jdbc:mysql://%s:%s/%s", "com.mysql.jdbc.Driver"), 
	Oracle("SELECT * FROM %s WHERE rownum < 1", "jdbc:oracle:thin:@%s:%s:%s", "oracle.jdbc.driver.OracleDriver"),
	SQLServer("SELECT * FROM %s WHERE 1 = 2", "jdbc:sqlserver://%s:%s;database=%s", "com.microsoft.sqlserver.jdbc.SQLServerDriver"), 
	PostgreSQL("SELECT * FROM \"%s\" WHERE 1 = 2", "jdbc:postgresql://%s:%s/%s", "org.postgresql.Driver"),
	SQLLite3("SELECT * FROM %s WHERE 1 = 2", "jdbc:sqlite:%s", "org.sqlite.JDBC"); // only database name

	private final String selectEmptyResultSQLTmpl; // 获取ResultSetMetaData的SQL语句
	private final String jdbcURLTmpl; // jdbcURL模板
	private final String jdbcDriverClassName; // jdbc驱动类名

	private DbType(String selectEmptyResultSQLTmpl, String jdbcURLTmpl, String jdbcDriverClassName) {
		this.selectEmptyResultSQLTmpl = selectEmptyResultSQLTmpl;
		this.jdbcURLTmpl = jdbcURLTmpl;
		this.jdbcDriverClassName = jdbcDriverClassName;
	}
	/**
	 * 获取ResultSetMetaData的SQL语句
	 */
	public String getSelectEmptyResultSQLTmpl() {
		return selectEmptyResultSQLTmpl;
	}
	/**
	 * 获取jdbcURL模板 {%s:ip, %s:port, %s:databaseName} SQLLite3只有%s:databaseName
	 */
	public String getJdbcURLTmpl() {
		return jdbcURLTmpl;
	}
	/**
	 * jdbc驱动类名
	 */
	public String getJdbcDriverClassName() {
		return jdbcDriverClassName;
	}
	
	/**
	 * 根据枚举的ordinal获取枚举对象
	 *  // TODO
	 * @param ordinal
	 */
	public static DbType getByOrdinal(int ordinal) {
		for (DbType item : values()) {
			if (item.ordinal() == ordinal) {
				return item;
			}
		}
		return null;
	}
	/**
	 * 获取所有枚举的名字
	 */
	public static String[] getNames() {
		String []names = new String[values().length];
		int i = 0;
		for (DbType item : values()) {
			names[i++] = item.name();
		}
		return names;
	}
	/**
	 * 获取所有枚举的jdbcUrl模板
	 */
	public static String[] getJdbcUrlTmpls() {
		String []urls = new String[values().length];
		int i = 0;
		for (DbType item : values()) {
			urls[i++] = item.getJdbcURLTmpl();
		}
		return urls;
	}
	/**
	 * 获取所有枚举的jdbc驱动类名
	 */
	public static String[] getJdbcDriverClassNames() {
		String []drivers = new String[values().length];
		int i = 0;
		for (DbType item : values()) {
			drivers[i++] = item.getJdbcDriverClassName();
		}
		return drivers;
	}
}
